package com.jack.Imitate;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;

/**
 * HttpClient模拟登录
 * 
 * @author Administrator
 * 
 */
public class ImitateLogin {
	/**
	 * 将内容保存到指定的HTML文件路径�?
	 * 
	 * @param data
	 * @param filePath
	 */
	private static void saveToLocal(byte[] data, String filePath) {
		try {
			DataOutputStream out = new DataOutputStream(new FileOutputStream(
					new File(filePath)));
			for (int i = 0; i < data.length; i++)
				out.write(data[i]);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 * @throws IOException
	 * @throws HttpException
	 */
	public static void main(String[] args) throws HttpException, IOException {
		// URL链接
		String url = "http://jiaowu.zhbit.com";
		// 保存
		String filePath = "F:\\1.html";

		// HTTP状�?�?
		int statusCode;

		// POST表单域�?
		NameValuePair[] data = {
				new NameValuePair("__VIEWSTATE",
						"dDwyODE2NTM0OTg7Oz5v+De3E3S3+ljsAqmagIDcdbdODw=="),
				new NameValuePair("txtUserName", "********"),
				new NameValuePair("TextBox2", "*******"),
				new NameValuePair("txtSecretCode", ""),
				new NameValuePair("RadioButtonList1", "学生"),
				new NameValuePair("Button1", ""),
				new NameValuePair("lbLanguage", ""),
				new NameValuePair("hidPdrs", ""),
				new NameValuePair("hidsc", "") };

		// 创建HttpClient实例
		HttpClient client = new HttpClient();
		// 创建GET方法实例
		GetMethod get = new GetMethod(url);
		// 执行GetMethod方法
		statusCode = client.executeMethod(get);

		System.out.println("第一次：GET\nHTTP Code:" + statusCode);
		// 释放POST链接
		get.releaseConnection();

		// 临时cookie保存
		String temp = null;
		// 获取cookie
		Cookie[] cookies = client.getState().getCookies();
		for (Cookie cookie : cookies) {
			temp = cookie.toString();
		}
		System.out.println(temp);

		// 创建POST方法实例
		PostMethod postMethod = new PostMethod(
				"http://jiaowu.zhbit.com/default2.aspx");
		//添加请求头部的cookie信息
		postMethod.addRequestHeader("Cookie", temp);
		//将表单的值放进post的请求body�?
		postMethod.setRequestBody(data);
		//执行post方法
		statusCode = client.executeMethod(postMethod);

		System.out.println("第二次：POST\nHTTP Code:" + statusCode);

		//处理页面重定向问�?01或�?302
		if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
				|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
			//获取相应头中�?location"信息
			Header locationHeader = postMethod.getResponseHeader("location");
			String location = null;
			if (locationHeader != null) {
				location = locationHeader.getValue();
				System.out.println("The page was redirected to:" + location);
				url += location;
			} else {
				System.err.println("Location field value is null.");
			}
		}
		postMethod.releaseConnection();
		
		//重定向URL
		System.out.println(url);
		
		// GET
		GetMethod get1 = new GetMethod(url);
		get1.addRequestHeader("Cookie", temp);
		statusCode = client.executeMethod(get1);
		System.out.println("第三次：GET\nHTTP Code:" + statusCode);

		// System.out.println(get1.getResponseBodyAsString());
		/*
		 * byte[] responseBody = get1.getResponseBody();//读取为字节数�?// 根据网页 url
		 * 生成保存时的文件�?saveToLocal(responseBody,filePath);
		 */
		get1.releaseConnection();
	}

}
